package com.douqu.game.main.util;

import com.douqu.game.core.factory.PlatformFactory;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.COSObjectSummary;
import com.qcloud.cos.model.GetObjectRequest;
import com.qcloud.cos.model.ListObjectsRequest;
import com.qcloud.cos.model.ObjectListing;
import com.qcloud.cos.region.Region;

import java.io.File;
import java.util.List;

/**
 * @Author: Bean
 * @Description:
 * @Date: 2018-01-28 20:44
 */
public class CosUtils {

    public static boolean copyLocalFile(String path, String targetPath)
    {
        File file = new File(path);
        if(file == null || !file.isDirectory())
            return false;

        File[] files = file.listFiles();
        if(files == null || files.length < 10)
            return false;

        try{
            // 将文件写入data
            File tempFile = new File(CoreUtils.getBasePath() + targetPath);
            if(!tempFile.exists())
                tempFile.mkdir();

            for (File f : files) {
                tempFile = new File(CoreUtils.getBasePath() + targetPath + "/" + f.getName());
                if(CoreUtils.getInternetIp().indexOf("192.168") == -1 && !tempFile.exists()){
                    tempFile = new File(CoreUtils.getRootPath() + targetPath + "/" + f.getName());
                }

                CoreUtils.copyFileUsingFileChannels(f, tempFile);
            }
            return true;
        }catch (Exception e){
            LogUtils.errorException(e);
            return false;
        }
    }

    public static boolean downloadFile(String bucket, String remotePath, String downloadPath)
    {
        COSClient cosClient = getCOSClient();
        try{
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
            listObjectsRequest.setBucketName(bucket);
            listObjectsRequest.setPrefix(remotePath);
            listObjectsRequest.setMaxKeys(200);
            ObjectListing objectListing = cosClient.listObjects(listObjectsRequest);
            List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
            if(cosObjectSummaries.size() == 0)
                return false;
            String remoteFile = "";
            String localFile = "";
            for(COSObjectSummary cosObjectSummary : cosObjectSummaries)
            {
                if(cosObjectSummary.getSize() == 0)
                    continue;

                remoteFile = cosObjectSummary.getKey();
                localFile = remoteFile.substring(remoteFile.lastIndexOf("/"));
                LogUtils.debug("下载配置文件 -> " + localFile);
                cosClient.getObject(new GetObjectRequest(PlatformFactory.TENCENT_COS_BUCKET, remoteFile), new File(downloadPath + localFile));
            }

        }catch (Exception e){
            LogUtils.errorException(e);

            return false;
        }finally {
            cosClient.shutdown();
        }

        return true;
    }

    public static COSClient getCOSClient()
    {
        COSCredentials cred = new BasicCOSCredentials(PlatformFactory.TENCENT_COS_SECRET_ID, PlatformFactory.TENCENT_COS_SECRET_KEY);
        ClientConfig clientConfig = new ClientConfig(new Region(PlatformFactory.TENCENT_COS_REGION));
        return new COSClient(cred, clientConfig);
    }




}
